/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seari.semodbus.client;

import com.seari.semodbus.bean.PLCDevice;
import com.seari.semodbus.utils.ModbusUtil;
import java.util.Arrays;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Rainbow
 */
public class PollingTask implements Runnable
{
    protected static Logger logger = LoggerFactory.getLogger(PollingTask.class);
    PLCDevice device;
    
    public PollingTask(PLCDevice device)
    {
        this.device = device;
    }

    @Override
    public void run()
    {
        while (true)
        {            
            int result[] = ModbusUtil.readRegister(device.getIp(), device.getPort(), device.getAddress(), device.getSlaveID(),device.getCount());
            new Thread(new RedisService(result, device)).run();
            //logger.debug(device.getIp()+":"+device.getPort()+" "+device.getDeviceSeq()+":result:"+Arrays.toString(result));
            try
            {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e)
            {
                try
                {
                    TimeUnit.SECONDS.sleep(2);
                } catch (Exception exception)
                {
                    
                }
            }

        }
    }
    
    public boolean close()
    {
        return false;
    }
    
}
